<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>观察者模式--腾讯面试题</title>
</head>
<body>
<script>
function Observer() {
    var publisher = {
        subscribers: {
            any: []
        },
        listen: function (eventName, handles) {
            handles=handles||'any';
            if (typeof this.subscribers[eventName]==='undefined') {
                this.subscribers[eventName]=[];
            }
            this.subscribers[eventName].push(handles);
        },
        remove:function(eventName){
            for(var i=0;i<subscribers[any].length;i++){
                if(subscribers.any[i]===eventName){
                    subscribers.any[i].splice(i,1);
                }
            }
        },
        trigger:function(eventName,fn){
            for(var i=0;i<this.subscribers.length;i++){
                if(this.subscribers[any][i]===eventName){
                    console.log(this.subscribers[any][i]);
                }
            }
        }
    };
    return publisher;
}
var p1=new Observer();
p1.listen('call1',function(){console.log('111')});
p1.trigger('call1');
</script>
</body>
</html>